package com.atguigui.leetcode;

import java.util.TreeMap;

/**
 * 1302.层数最深叶子节点的和
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/8/17 9:18
 */
public class P1302DeepestLeavesSum {
    public static void main(String[] args) {
        Solution solution = new P1302DeepestLeavesSum().new Solution();
        // TO TEST
    }


    class Solution {
        private TreeMap<Integer, Integer> map = new TreeMap();

        public int deepestLeavesSum(TreeNode root) {
            getDeep(root, 0);
            return map.get(map.lastKey());
        }

        private void getDeep(TreeNode root, int n) {
            if (root == null) {
                return;
            }
            Integer nums = map.getOrDefault(n, 0);
            map.put(n, nums + root.val);

            n++;
            getDeep(root.left, n);
            getDeep(root.right, n);
        }
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
